package com.hckjsec.cloud.service.impl;

import java.util.List;
import java.util.Map;

import com.hckjsec.cloud.domain.AppletsWechatUser;
import com.hckjsec.cloud.domain.WebWebsiteUser;
import com.hckjsec.cloud.domain.wechatView.OpenidView;
import com.hckjsec.cloud.service.IAppletsWechatUserService;
import com.hckjsec.cloud.service.IWebWebsiteUserService;
import com.hckjsec.common.core.redis.RedisCache;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hckjsec.cloud.mapper.AppletsApplicantUserMapper;
import com.hckjsec.cloud.domain.AppletsApplicantUser;
import com.hckjsec.cloud.service.IAppletsApplicantUserService;

/**
 * 微信用户人员信息Service业务层处理
 * 
 * @author cyt
 * @date 2021-08-10
 */
@Service
public class AppletsApplicantUserServiceImpl implements IAppletsApplicantUserService 
{
    @Autowired
    private AppletsApplicantUserMapper appletsApplicantUserMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IAppletsWechatUserService appletsWechatUserService;

    @Autowired
    private IWebWebsiteUserService webWebsiteUserService;

    /**
     * 查询微信用户人员信息
     * 
     * @param id 微信用户人员信息ID
     * @return 微信用户人员信息
     */
    @Override
    public AppletsApplicantUser selectAppletsApplicantUserById(Long id)
    {
        return appletsApplicantUserMapper.selectAppletsApplicantUserById(id);
    }

    /**
     * 查询微信用户人员信息列表
     * 
     * @param appletsApplicantUser 微信用户人员信息
     * @return 微信用户人员信息
     */
    @Override
    public List<AppletsApplicantUser> selectAppletsApplicantUserList(AppletsApplicantUser appletsApplicantUser)
    {
        return appletsApplicantUserMapper.selectAppletsApplicantUserList(appletsApplicantUser);
    }

    /**
     * 新增微信用户人员信息
     * 
     * @param appletsApplicantUser 微信用户人员信息
     * @return 结果
     */
    @Override
    public int insertAppletsApplicantUser(AppletsApplicantUser appletsApplicantUser)
    {
        return appletsApplicantUserMapper.insertAppletsApplicantUser(appletsApplicantUser);
    }

    /**
     * 修改微信用户人员信息
     * 
     * @param appletsApplicantUser 微信用户人员信息
     * @return 结果
     */
    @Override
    public int updateAppletsApplicantUser(AppletsApplicantUser appletsApplicantUser)
    {
        return appletsApplicantUserMapper.updateAppletsApplicantUser(appletsApplicantUser);
    }

    /**
     * 批量删除微信用户人员信息
     * 
     * @param ids 需要删除的微信用户人员信息ID
     * @return 结果
     */
    @Override
    public int deleteAppletsApplicantUserByIds(Long[] ids)
    {
        return appletsApplicantUserMapper.deleteAppletsApplicantUserByIds(ids);
    }

    /**
     * 删除微信用户人员信息信息
     * 
     * @param id 微信用户人员信息ID
     * @return 结果
     */
    @Override
    public int deleteAppletsApplicantUserById(Long id)
    {
        return appletsApplicantUserMapper.deleteAppletsApplicantUserById(id);
    }

    /**
     * 查询微信用户人员信息
     *
     * @param openid 微信用户人员信息openid
     * @return 微信用户人员信息
     */
    @Override
    public AppletsApplicantUser selectAppletsApplicantUserByOpenId(String openid) {
        return appletsApplicantUserMapper.selectAppletsApplicantUserByOpenId(openid);
    }

    /**
     * 验证redis中openid对象
     *
     * @param openid 微信用户人员信息openid
     * @return 微信用户人员信息
     */
    public OpenidView getOpenidView(String openid,boolean falg){
       //先验证redis是否存在
        OpenidView openidViewNew =redisCache.getCacheObject(ComUtil.getWechatCacheKey(openid));
        if (!StringUtils.isNull(openidViewNew) && !falg){
            return openidViewNew;
        }
        //如果不存在  就重新封装
        OpenidView openidView = new OpenidView();
        AppletsWechatUser appletsWechatUserData = appletsWechatUserService.selectAppletsWechatUserById(openid);
        openidView.setOpenId(appletsWechatUserData.getOpenid());
        openidView.setDeptId(appletsWechatUserData.getDeptId());
        openidView.setConfigId(appletsWechatUserData.getConfigId());
        openidView.setWechatName(appletsWechatUserData.getNickname());
        openidView.setWechatImage(appletsWechatUserData.getAvatarurl());
        openidView.setAreatype(appletsWechatUserData.getAreatype());
        AppletsApplicantUser appletsApplicantUser=appletsApplicantUserMapper.selectAppletsApplicantUserByOpenId(openid);
        if (!StringUtils.isNull(appletsApplicantUser)){
            openidView.setUserId(appletsApplicantUser.getId());
            openidView.setRealName(appletsApplicantUser.getName());
            openidView.setWechatPhone(appletsApplicantUser.getPhone());
        }
        redisCache.setCacheObject(ComUtil.getWechatCacheKey(openid),openidView);
        return openidView;
    }


    /**
     * 验证redis中openid对象
     *
     * @param openid 网站用户人员信息openid
     * @return 微信用户人员信息
     */
    public OpenidView getOpenidWebView(String openid,boolean falg){
        //先验证redis是否存在
        OpenidView openidViewNew =redisCache.getCacheObject(ComUtil.getWechatCacheKey(openid));
        if (!StringUtils.isNull(openidViewNew) && !falg){
            return openidViewNew;
        }
        //如果不存在  就重新封装
        OpenidView openidView = new OpenidView();
        WebWebsiteUser webWebsiteUser = webWebsiteUserService.selectWebWebsiteUserById(openid);
        openidView.setOpenId(webWebsiteUser.getWebId());
        openidView.setDeptId(webWebsiteUser.getDeptId());
        openidView.setConfigId(webWebsiteUser.getConfigId());
        AppletsApplicantUser appletsApplicantUser=appletsApplicantUserMapper.selectAppletsApplicantUserByOpenId(openid);
        if (!StringUtils.isNull(appletsApplicantUser)){
            openidView.setUserId(appletsApplicantUser.getId());
            openidView.setRealName(appletsApplicantUser.getName());
            openidView.setWechatPhone(appletsApplicantUser.getPhone());
            openidView.setAreatype(appletsApplicantUser.getAreatype());
        }
        redisCache.setCacheObject(ComUtil.getWechatCacheKey(openid),openidView);
        return openidView;
    }


    /**
     * 通过id获取list对象
     *
     * @param openid 微信用户人员信息openid
     * @return 微信用户人员信息
     */
    @Override
    public List<AppletsApplicantUser> selectAppletsApplicantUserByIdList(Long[] ids) {
        return appletsApplicantUserMapper.selectAppletsApplicantUserByIdList(ids);
    }

    @Override
    public List<AppletsApplicantUser> selectAppletsApplicantUserUrlList(AppletsApplicantUser appletsApplicantUser) {
        return appletsApplicantUserMapper.selectAppletsApplicantUserUrlList(appletsApplicantUser);
    }


    /**
     * 查询机构对应人员数量
     *
     * @param deptId 申办信息
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findDeptIdByUserNumMap(AppletsApplicantUser appletsApplicantUser) {
        return appletsApplicantUserMapper.findDeptIdByUserNumMap(appletsApplicantUser);
    }
}
